#include<iostream>
#include<vector>
#include<string>
#include<cstdlib>
#include<fstream>

using namespace std;

struct Person
{
    string name;
    string num;
};

vector<Person> addressbook;
void enter()
{
    if(addressbook.size()==0)
    {
        cout<<"当前通讯录为空"<<endl;

    }
    cout<<"是否录入？"<<endl;
    cout<<"1、是"<<endl;
    cout<<"2、否"<<endl;
    int choose;
    cin>>choose;
    switch(choose)
    {
        case 1:
        {
            Person temp;
            cout<<"请输入姓名：";
            cin>>temp.name;
            cout<<"请输入电话号码：";
            cin>>temp.num;
            addressbook.push_back(temp);
            cout<<"添加成功"<<endl;
        }
        break;
        case 2:
        {
            return;

        }
        break;
    }
}
void del()
{
    if(addressbook.size()==0)
    {
        cout<<"当前通讯录为空"<<endl;
        return;
    }
int temp=addressbook.size();
    cout<<"请输入要删除的联系人的姓名：";
    Person t;
    cin>>t.name;
    for(int i=0;i<addressbook.size();++i)
    {
        if(addressbook[i].name==t.name)
        {
            cout<<addressbook[i].name<<"    "<<addressbook[i].num<<endl;
            int choose;
            cout<<"是否删除？"<<endl;
            cout<<"1、是"<<endl;
            cout<<"2、否"<<endl;
            cin>>choose;
            switch(choose)
            {
                case 1:
                {
            auto it=addressbook.begin()+i;
            addressbook.erase(it);
            cout<<"删除成功"<<endl;
            
                }
                break;
                case 2:
                break;
            }
            
        }
    }
    if(temp==addressbook.size())
    cout<<"查无此人"<<endl;
    
}

void list()
{
    if(addressbook.size()==0)
    {
        cout<<"当前通讯录为空"<<endl;
        return;
    }

    for(int i=0;i<addressbook.size();++i)
    {
        cout<<addressbook[i].name<<"    "<<addressbook[i].num<<endl;
        
    }
}

void search()
{
    int temp=0;
    cout<<"请输入要查找的联系人的姓名：";
    Person t;
    cin>>t.name;
    for(int i=0;i<addressbook.size();++i)
    {
        if(addressbook[i].name==t.name)
        {
            cout<<addressbook[i].name<<"    "<<addressbook[i].num<<endl;
            temp=1;
        }
    }
if(temp==0)
    cout<<"查无此人"<<endl;
    
}


void save()
{
    string path;
    cout<<"请输入文件路径：";
    cin>>path;
    ofstream outfile;
    outfile.open(path,ios::app);
    if(!outfile.is_open())
    {
        cout<<"打开文件失败"<<endl;
        return;
    }
    cout<<"Saving file"<<endl;
    for(int i=0;i<addressbook.size();++i)
    {
        outfile<<addressbook[i].name<<endl;
        outfile<<addressbook[i].num<<endl;
    }
    outfile.close();
    cout<<"保存完成"<<endl;

}

void load()
{
    string path;
    cout<<"请输入文件路径：";
    cin>>path;
    ifstream infile;
    infile.open(path,ios::in);
    if(!infile.is_open())
    {
        cout<<"打开文件失败"<<endl;
        return;
    }
    
    string line;
    int check=0;
    Person temp;
    while(getline(infile,line))
    {
        if(check%2==0)
        {
            temp.name=line;
        }
        else if(check%2==1)
        {
            temp.num=line;
            addressbook.push_back(temp);
        }
        check++;
    }

    infile.close();
    cout<<"加载完成"<<endl;

}
int main()

{

    while(1)
    {
        cout<<"-----通讯录管理系统-----"<<endl;
        cout<<"1、通讯录录入"<<endl;
        cout<<"2、通讯录删除"<<endl;
        cout<<"3、通讯录列表显示"<<endl;
        cout<<"4、通讯录查询"<<endl;
        cout<<"5、通讯录数据保存"<<endl;
        cout<<"6、通讯录数据加载"<<endl;
        cout<<"7、退出系统"<<endl;
        int n;
        cin>>n;
        switch(n)
        {
            case 1:
            {
                enter();
                system("pause");
                system("cls");
            }
            break;
            case 2:
            {
                del();
                system("pause");
                system("cls");
            }
            break;
            case 3:
            {
                list();
                system("pause");
                system("cls");
            }
            break;
            case 4:
            {
                search();
                system("pause");
                system("cls");
            }
            break;
            case 5:
            {
                save();
                system("pause");
                system("cls");
            }
            break;
            case 6:
            {
                load();
                system("pause");
                system("cls");
            }
            break;
            case 7:
            {
                cout<<"退出成功"<<endl;
                return 0;
            }
            break;
            
        }
    }







    return 0;
}